package lib.paxos;

import java.io.Serializable;

import lib.ArgumentValidator;
import lib.Command;

/**
 * Proposer uses decide message to convey to acceptors,
 * that a value has been chosen globally.
 * Acceptors can then consider the paxos round to be over,
 * and execute the chosen command
 * @author abhip
 *
 */
public class DecideMessage implements Serializable
{

    /**
     * serialVersionUID is a universal version identifier for a Serializable
     * class. Deserialization uses this number to ensure that a loaded class
     * corresponds exactly to a serialized object. If no match is found during
     * deserialization, an InvalidClassException is thrown.
     */
    private static final long serialVersionUID = -733972797072109274L;

    private Command command;
    private int roundID;
    private SequenceID sequenceID;

    public DecideMessage(
            int roundID,
            SequenceID sequenceID,
            Command command)
    {
        ArgumentValidator.IsGreaterThanOrEqualTo("roundID", roundID, 0);
        ArgumentValidator.IsNotNull("sequenceID", sequenceID);
        ArgumentValidator.IsNotNull("command", command);

        this.roundID = roundID;
        this.sequenceID = sequenceID;
        this.command = command;
    }

    public Command Command()
    {
        return command;
    }

    public int RoundID()
    {
        return roundID;
    }

    public SequenceID SequenceID()
    {
        return sequenceID;
    }

    @Override
    public String toString()
    {
        return String.format(
                "roundID: %d, sequenceID: %s, command:%s",
                roundID,
                sequenceID.toString(),
                command.toString());
    }
}
